//
//  MKVisitorView.swift
//  MKWeiBo2017
//
//  Created by nicolas on 2017/5/9.
//  Copyright © 2017年 nicolas. All rights reserved.
//

import UIKit

//1、定义协议
protocol MKVisitorViewDelegate: NSObjectProtocol {
    //2、定义协议方法
    func visitorViewDidClickRegister()
    func visitorViewDidClickLogin()
}

class MKVisitorView: UIView {
    //3、定义代理属性
    weak var delegate: MKVisitorViewDelegate?
    
    //UIKit的里面的view即可以通过代码来创建，页可以通过xib/sb来创建
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //构造函数，看父类有没有和这个一样的构造函数
    override init(frame: CGRect) {
        //在super.init之前给属性赋值
        //调用对象方法不能放到super.init之前，在super.init之前，这个对象还没初始化
        super.init(frame: frame)
        
        prepareUI()
    }
    
    //转轮旋转动画
    func startRotationAnimation() {
        //创建核心动画
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.toValue = M_PI * 2
        anim.duration = 20
        anim.repeatCount = MAXFLOAT
        anim.isRemovedOnCompletion = false
        //将核心动画添加到图层上
        iconView.layer.add(anim, forKey: nil)
        
    }
    
    //访客视图内容切换
    func setupVistorInfo(imageName: String, message: String) {
        iconView.image = UIImage(named: imageName)
        messageLabel.text = message
        homeView.isHidden = true
    }
    //注册点击事件
    //按钮点击事件方法是系统来调用的，当把按钮点击事件private后系统找不到这个方法了
    //@objc：让系统可以找到我们的方法
    @objc private func didClickRegister (button: UIButton) {
        //4、调用代理的方法
        delegate?.visitorViewDidClickRegister()
    }
    //登录点击事件
    func didClickLogin (button: UIButton) {
        delegate?.visitorViewDidClickLogin()
    }
    
    private func prepareUI() {
        
        self.backgroundColor = UIColor(white: 237 / 255.0, alpha: 1)
        
        self.addSubview(iconView)
        self.addSubview(coverView)
        self.addSubview(homeView)
        self.addSubview(messageLabel)
        self.addSubview(registerButton)
        self.addSubview(loginButton)
        
        iconView.translatesAutoresizingMaskIntoConstraints = false
        coverView.translatesAutoresizingMaskIntoConstraints = false
        homeView.translatesAutoresizingMaskIntoConstraints = false
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        registerButton.translatesAutoresizingMaskIntoConstraints = false
        loginButton.translatesAutoresizingMaskIntoConstraints = false
        
        //添加约束（最好只添加一次）
        //centerX和父控件的centerX重合
        //item:要添加约束的view
        //attribute:要添加的view的属性
        //toItem：要参照的view
        //attribute:要参照view的属性
        
        //转轮
        self.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.centerX, relatedBy: NSLayoutRelation.equal, toItem: self, attribute: NSLayoutAttribute.centerX, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.centerY, relatedBy: NSLayoutRelation.equal, toItem: self, attribute: NSLayoutAttribute.centerY, multiplier: 1, constant: -40))
        //遮盖
        self.addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.leading, relatedBy: NSLayoutRelation.equal, toItem: self, attribute: NSLayoutAttribute.leading, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.top, relatedBy: NSLayoutRelation.equal, toItem: self, attribute: NSLayoutAttribute.top, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.trailing, relatedBy: NSLayoutRelation.equal, toItem: self, attribute: NSLayoutAttribute.trailing, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.bottom, relatedBy: NSLayoutRelation.equal, toItem: registerButton, attribute: NSLayoutAttribute.bottom, multiplier: 1, constant: 0))
        
        
        //小房子
        self.addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.centerX, relatedBy: NSLayoutRelation.equal, toItem: iconView, attribute: NSLayoutAttribute.centerX, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.centerY, relatedBy: NSLayoutRelation.equal, toItem: iconView, attribute: NSLayoutAttribute.centerY, multiplier: 1, constant: 0))
        //消息label
        self.addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.width, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: 240))
        self.addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.centerX, relatedBy: NSLayoutRelation.equal, toItem: self, attribute: NSLayoutAttribute.centerX, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.top, relatedBy: NSLayoutRelation.equal, toItem: iconView, attribute: NSLayoutAttribute.bottom, multiplier: 1, constant: 16))
        //注册按钮
        self.addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.top, relatedBy: NSLayoutRelation.equal, toItem: messageLabel, attribute: NSLayoutAttribute.bottom, multiplier: 1, constant: 16))
        self.addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.leading, relatedBy: NSLayoutRelation.equal, toItem: messageLabel, attribute: NSLayoutAttribute.leading, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.width, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: 100))
        self.addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: 35))
        //登录按钮
        self.addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.top, relatedBy: NSLayoutRelation.equal, toItem: messageLabel, attribute: NSLayoutAttribute.bottom, multiplier: 1, constant: 16))
        self.addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.trailing, relatedBy: NSLayoutRelation.equal, toItem: messageLabel, attribute: NSLayoutAttribute.trailing, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.width, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: 100))
        self.addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: 35))
        
    }
    
    //转轮
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    //遮盖视图
    private lazy var coverView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    //小房子
    private lazy var homeView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    //消息文字
    private lazy var messageLabel: UILabel = {
        let label = UILabel()
        
        label.text = "关注一些人，看看有什么惊喜"
        label.textColor = UIColor.black
        label.font = UIFont.systemFont(ofSize: 16)
        label.textAlignment = NSTextAlignment.center
        label.numberOfLines = 0
        label.sizeToFit()
        
        return label
    }()
    
    //注册按钮
    private lazy var registerButton: UIButton = {
        let button = UIButton()
        
        button.setBackgroundImage(UIImage(named:"common_button_white_disable"), for: UIControlState.normal)
        button.setTitleColor(UIColor.orange, for: UIControlState.normal)
        button.setTitle("注册", for: UIControlState.normal)
        button.addTarget(self, action: #selector(didClickRegister(button:)), for: .touchUpInside)
        return button
    }()
    
    //登录按钮
    private lazy var loginButton:UIButton  = {
        let button = UIButton()
        
        button.setBackgroundImage(UIImage(named:"common_button_white_disable"), for: UIControlState.normal)
        button.setTitleColor(UIColor.black, for: UIControlState.normal)
        button.setTitle("登录", for: UIControlState.normal)
         button.addTarget(self, action: #selector(didClickLogin(button:)), for: .touchUpInside)
        return button
        
    }()
}
